package com.nf.health.app.inquiry.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.customview.HeadListView;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.inquiry.bean.InquiryBean;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 提问适配器
 * Created by Administrator on 2016/4/27.
 */
public class InquiryHaveCompleteAdapter extends BaseAdapter implements SectionIndexer, HeadListView.HeaderAdapter, AbsListView.OnScrollListener {

    public ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

    List<InquiryBean> newsList;
    Activity activity;
    LayoutInflater inflater = null;


    String icon, name;

    public InquiryHaveCompleteAdapter(Activity activity, List<InquiryBean> newsList) {
        this.activity = activity;
        this.newsList = newsList;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return newsList == null ? 0 : newsList.size();
    }

    @Override
    public Object getItem(int position) {
        if (newsList != null && newsList.size() != 0) {
            return newsList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final InquiryBean item = newsList.get(position);
        ViewHolder holder;
        holder = ViewHolder.get(parent, convertView, R.layout.item_question_tiwen1, position);
        //问诊人信息
        holder.setText(R.id.tv_patient_info, item.getUserdesc());
        //问诊内容,最多两行半
        String res = item.getContentBean().getMessage();
        TextView inquiryView = holder.getView(R.id.tv_inquiry_info);
        String subRes = ViewUtil.getSubString(inquiryView, res, 1, activity);
        inquiryView.setText(subRes);
        //问诊图片提示
        TextView tvHaveImage = holder.getView(R.id.tv_have_image);
        if (item.getContentBean().getImages().size() != 0) {
            tvHaveImage.setVisibility(View.VISIBLE);
        } else {
            tvHaveImage.setVisibility(View.INVISIBLE);
        }
        //时间
        holder.setText(R.id.tv_date, DateUtil.date2String(item.getUpdatetime(), "MM-dd"));

        //侧滑删除
        SwipeView swipeView = holder.getView(R.id.swipeView);
        View delete = holder.getView(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(position, "delete");
                unClosedSwipeView.clear();
            }
        });

        swipeView.setOnSwipeStatusChangeListener(new SwipeView.OnSwipeStatusChangeListener() {

            @Override
            public void onSwiping(SwipeView swipingSwipeView) {
                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    closeAllOpenedSwipeView();
                }

                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    unClosedSwipeView.add(swipingSwipeView);
                }

            }

            @Override
            public void onOpen(SwipeView openedSwipeView) {
                for (int i = 0; i < unClosedSwipeView.size(); i++) {
                    if (unClosedSwipeView.get(i) != openedSwipeView) {
                        unClosedSwipeView.get(i).close();
                    }
                }
                if (!unClosedSwipeView.contains(openedSwipeView)) {
                    unClosedSwipeView.add(openedSwipeView);
                }
            }

            @Override
            public void onClose(SwipeView closedSwipeView) {
                unClosedSwipeView.remove(closedSwipeView);
            }
        });
        //再次包裹一层才能相应点击事件,swipeView不相应 为什么???
        LinearLayout ll_content = holder.getView(R.id.ll_content);
        ll_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(position, "click");
                unClosedSwipeView.clear();
            }
        });

        return holder.getConvertView();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    //滑动监听
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
    }

    @Override
    public int getHeaderState(int position) {
        int realPosition = position;
        if (realPosition < 0 || position >= getCount()) {
            return HEADER_GONE;
        }
        int section = getSectionForPosition(realPosition);
        int nextSectionPosition = getPositionForSection(section + 1);
        if (nextSectionPosition != -1
                && realPosition == nextSectionPosition - 1) {
            return HEADER_PUSHED_UP;
        }
        return HEADER_VISIBLE;
    }

    @Override
    public void configureHeader(View header, int position, int alpha) {
        // TODO Auto-generated method stub

    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int section) {
        return 0;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    private OnMyClickListener listener;

    public void setOnMyClickListener(OnMyClickListener listener) {
        this.listener = listener;
    }

    private void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
}